package com.ymt.reliable.message.confirm.timer;

import com.ymt.reliable.message.confirm.config.MessageConfirmConfig;
import com.ymt.reliable.message.confirm.service.MessageConfirmService;
import com.ymt.reliable.message.confirm.task.Task;
import com.ymt.reliable.message.confirm.task.TaskFactory;
import com.ymt.reliable.message.domain.TransactionMessage;
import com.ymt.reliable.message.service.TransactionMessageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class MessageConfirmTimer {
    private static final Logger LOGGER = LoggerFactory.getLogger(MessageConfirmTimer.class);

    @Autowired
    private TransactionMessageService transactionMessageService;
    @Autowired
    private MessageConfirmService messageConfirmService;
    @Autowired
    private MessageConfirmConfig config;

    public void confirm() {
        List<TransactionMessage> confirmList = transactionMessageService.queryConfirmList(config.getTimeout(), config.getMaxconfirmtimes());
        LOGGER.info(">>> 积压待确认消息总数:{},延迟队列中任务总数:{}", confirmList == null ? 0 : confirmList.size(), messageConfirmService.queueSize());
        if (confirmList != null && confirmList.size() > 0) {
            for (TransactionMessage message : confirmList) {
                Task task = TaskFactory.createTask(message, config);
                messageConfirmService.addTask(task);
            }
        }
    }
}
